package com.starblink.guangconfig.ui.part

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.starblink.android.basic.bridge.BridgeCommand
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.config.GuangConfig
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.network.base.ApiSiteEnum
import com.starblink.android.basic.network.base.BaseApiCodeAndUrl
import com.starblink.android.basic.network.base.BaseApiConfig
import com.starblink.android.basic.network.base.BaseSiteSwitch
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.util.ActivityStackManage
import com.starblink.guangconfig.databinding.ActivityGuangUpConfigBinding
import com.starblink.guangconfig.ui.GuangUpConfigVM

object EnvironmentSwitchPart {

    private var checkStatus = hashMapOf(
        ApiSiteEnum.GB.name to false,
        ApiSiteEnum.PUB.name to false,
        ApiSiteEnum.PRE.name to false,
        ApiSiteEnum.TEST.name to false,
        ApiSiteEnum.DEV.name to false
    )

    fun handleVM(context: Context, m: GuangUpConfigVM, vb: ActivityGuangUpConfigBinding) {

        //注意 part是单例哦 生命周期全周期
        if (BaseSiteSwitch.canSwitchPub()) {
            vb.environmentSwitch.ckPub.visible()
            vb.environmentSwitch.ckPubGb.visible()
        } else {
            vb.environmentSwitch.ckPub.gone()
            vb.environmentSwitch.ckPubGb.gone()
        }

        var svrApiCfgEnv = BaseApiConfig.svrApiCfgEnv

        val currentDev = (svrApiCfgEnv[BaseApiCodeAndUrl.NAME] as String).uppercase()
        val siteDev1 = svrApiCfgEnv[BaseApiCodeAndUrl.SITE] as String

        //非默认站点单独处理
        if (siteDev1 == BaseApiConfig.SITE_NOT_NOT_DEFAULT) {
            checkStatus[siteDev1.uppercase()] = true
            refreshAllCheckBox(siteDev1.uppercase(), vb)
        }

        if (siteDev1 != BaseApiConfig.SITE_NOT_NOT_DEFAULT) {
            checkStatus[currentDev] = true
            refreshAllCheckBox(currentDev, vb)
        }




        vb.environmentSwitch.ckPub.setOnClickListener {
            resetData(context, ApiSiteEnum.PUB.name, m, vb)
        }
        vb.environmentSwitch.ckPubGb.setOnClickListener {
            resetData(context, ApiSiteEnum.GB.name, m, vb)
        }
        vb.environmentSwitch.ckPre.setOnClickListener {
            resetData(context, ApiSiteEnum.PRE.name, m, vb)
        }
        vb.environmentSwitch.ckTest.setOnClickListener {
            resetData(context, ApiSiteEnum.TEST.name, m, vb)
        }
        vb.environmentSwitch.ckDev.setOnClickListener {
            resetData(context, ApiSiteEnum.DEV.name, m, vb)
        }

        FlowBus.with<Boolean>(FlowConst.SWITCH_SITE_CI_CTL).register(vb.environmentSwitch.ckPre) {
            if (it) {
                vb.environmentSwitch.ckPub.visible()
                vb.environmentSwitch.ckPubGb.visible()
            } else {
                vb.environmentSwitch.ckPub.gone()
                vb.environmentSwitch.ckPubGb.gone()
            }
        }
    }

    private fun resetData(
        context: Context,
        currentDev: String,
        m: GuangUpConfigVM,
        vb: ActivityGuangUpConfigBinding
    ) {
        checkStatus.forEach {
            checkStatus[currentDev] = it.key == currentDev
        }
        refreshAllCheckBox(currentDev, vb)
        BaseSiteSwitch.switchSite(currentDev, callback = {
            toast("网络切换成功！2s后重启")
            FlowCountDownTimer(time = 2).start(callBack = {
            }, onFinish = {
                FlowBus.with<Boolean>(FlowConst.DEV_SWITCH).post(m.viewModelScope, true)
                ActivityStackManage.killAppAndRestart()
            })
//
        })
        //跨进程切换Web进程站点
        BridgeUtil.sendCommonData(BridgeCommand.Common.switchSite, currentDev)
    }

    private fun refreshAllCheckBox(currentDev: String, vb: ActivityGuangUpConfigBinding) {
        checkStatus.forEach {
            if (it.key == currentDev) {
                when (it.key) {
                    ApiSiteEnum.GB.name -> vb.environmentSwitch.ckPubGb.isChecked = true
                    ApiSiteEnum.PUB.name -> vb.environmentSwitch.ckPub.isChecked = true
                    ApiSiteEnum.PRE.name -> vb.environmentSwitch.ckPre.isChecked = true
                    ApiSiteEnum.TEST.name -> vb.environmentSwitch.ckTest.isChecked = true
                    ApiSiteEnum.DEV.name -> vb.environmentSwitch.ckDev.isChecked = true
                }
            } else {
                when (it.key) {
                    ApiSiteEnum.GB.name -> vb.environmentSwitch.ckPubGb.isChecked = false
                    ApiSiteEnum.PUB.name -> vb.environmentSwitch.ckPub.isChecked = false
                    ApiSiteEnum.PRE.name -> vb.environmentSwitch.ckPre.isChecked = false
                    ApiSiteEnum.TEST.name -> vb.environmentSwitch.ckTest.isChecked = false
                    ApiSiteEnum.DEV.name -> vb.environmentSwitch.ckDev.isChecked = false
                }
            }
        }
    }


}